package com.maimao.goods.controller;

import com.baidu.fsg.uid.impl.CachedUidGenerator;
import com.maimao.api.mall.goods.CommentApi;
import com.maimao.core.constant.ListData;
import com.maimao.core.model.auth.AuthUser;
import com.maimao.core.model.resp.PageResult;
import com.maimao.core.model.resp.Result;
import com.maimao.core.utils.DateUtils;
import com.maimao.goods.controller.base.BaseController;
import com.maimao.goods.service.CommentService;
import com.maimao.model.echarts.common.ChartsResult;
import com.maimao.model.echarts.common.LineOption;
import com.maimao.model.goods.Comment;
import com.maimao.model.goods.input.CommentFilter;
import com.maimao.model.goods.input.CommentInput;
import com.maimao.model.goods.vo.CommentSingle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author MaoLin Wang
 * @date 2021/1/28 10:27 上午
 */
@RestController
public class CommentController extends BaseController implements CommentApi {
    @Autowired
    private CachedUidGenerator cachedUidGenerator;

    @Autowired
    private CommentService commentService;


    /**
     * 发布评论
     */
    @PostMapping("/publish")
    @Override
    public Result<?> publish(@Valid @RequestBody CommentInput comment, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return entityInvalid(bindingResult);
        }
        AuthUser authUser = checkAndGetAuthUser();
        comment.setIsDefault(0);
        comment.setId(cachedUidGenerator.getUID());
        comment.setNeedExplain(comment.getType().equals(1) ? 0 : 1);
        comment.setReplied(0);
        comment.setUsername(authUser.getUsername());
        comment.setUserId(authUser.getId2Long());
        comment.setCreateTime(DateUtils.now());
        System.out.println(comment);
        return commentService.saveComment(comment);
    }

    /**
     * 回复评论
     */
    @PostMapping("/reply")
    public Result<?> reply(@Valid @RequestBody CommentInput comment, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return entityInvalid(bindingResult);
        }
        AuthUser authUser = checkAndGetAuthUser();

        setCommentValue(comment, authUser);


        System.out.println(comment);
        return commentService.replyComment(comment, authUser.getId2Long());
    }

    private void setCommentValue(@RequestBody @Valid CommentInput comment, AuthUser authUser) {
        comment.setIsDefault(0);
        comment.setId(cachedUidGenerator.getUID());
        comment.setNeedExplain(0);
        comment.setSpec("");
        comment.setGoodsId(null);
        comment.setSkuId(null);
        comment.setHasPicture(0);
        comment.setStar(0);
        comment.setType(0);
        comment.setUsername(authUser.getUsername());
        comment.setUserId(authUser.getId2Long());
        comment.setCreateTime(DateUtils.now());
    }

    /**
     * 查询评论中心店铺评论
     */
    @PostMapping("/ms-comments")
    public Result<?> msCommentsQuery(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "10") Integer size,
            @RequestBody CommentFilter filter) {
        AuthUser authUser = checkAndGetAuthUser();
        if (authUser.getShopId() == null) {
            return Result.notValid("店铺异常");
        }
        PageResult<Comment> res = commentService.queryByShop(authUser.getShopId(), filter, page, size);
        return Result.okData(res);
    }

    /**
     * 商品详情页评论数据
     *
     * @param goodsId 商品id
     * @return
     */
    @PostMapping("/goods-comments")
    @Override
    public Result<?> goodsDetailCommentData(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "20") Integer size,
            @RequestParam(value = "goodsId") Long goodsId, @RequestBody CommentFilter filter) {
        if (goodsId == null || goodsId <= 0) {
            return Result.notValid();
        }
        Map<String, Object> res = commentService.queryByGoods(goodsId, filter, page, size);
        return Result.okData(res);
    }

    /**
     * 单个评论的信息（包含所有的回复）
     *
     * @return
     */
    @GetMapping("/comment-one")
    @Override
    public Result<?> queryCommentById(@RequestParam(value = "id") Long id) {
        if (id == null || id <= 0) {
            return Result.notValid();
        }
        CommentSingle res = commentService.queryById(id);
        return Result.okData(res);
    }

    /**
     * 商品评论热词
     *
     * @param goodsId
     * @return
     */
    @Override
    @GetMapping("/hot-word")
    public Result<?> shopCommentHotWord(@RequestParam("goodsId") Long goodsId) {
        if (goodsId == null || goodsId <= 0) {
            return Result.okData(ListData.EMPTY_LIST);
        }
        return Result.okData(commentService.shopCommentHotWord(goodsId));
    }

    /**
     * 店铺评价统计 7日内好中差的评价数 一个月内好中差的评价数 半年内好中差的评价数 累计评价数
     */
    @GetMapping("/comment-agg")
    public Result<?> shopCommentAgg() {
        AuthUser authUser = checkAndGetAuthUser();
        Long shopId = authUser.getShopId();
        if (shopId == null || shopId <= 0) {
            return Result.notValid("店铺异常");
        }
        Map<String, Object> res = commentService.shopCommentAgg(shopId);
        return Result.okData(res);
    }

    /**
     * 后台评论数据概览 （内外圆饼图） 外饼图：好中差评总数 内环：带图和文字评论总数 评论总数
     *
     * @return
     */
    @GetMapping("/pie")
    public Result<?> msCommentDataOverView() {
        ChartsResult<Map<String, Object>> overview = commentService.overview();
        return Result.okData(overview);
    }

    /**
     * 后台评论数据折线/柱状图
     *
     * @return
     */
    @GetMapping("/line-bar")
    public Result<?> lineBarData(@RequestParam(value = "aggTimeType", required = false) Integer aggTimeType) {
        LineOption<Integer> line = commentService.line(aggTimeType);
        return Result.okData(line);
    }
}
